﻿using System;

namespace AgFractal.Fractals
{
    public class Magnet1 : AbstractFractal
    {
        public Magnet1()
        {
            ID = new Guid("F3AA7041-31FE-4e65-A3B0-AECFC394FC9F");
            Name = Description= "Magnet1";
            R1 = -1.5; I1 = 2.5; R2 = 4.5; I2 = -2.5;
            
            var cParam = new FractalParameter { Name = "c", ComplexValue = c };
            cParam.PropertyChanged += (s, e) => c = cParam.ComplexValue;
            parameter.Add(cParam);
        }

        private Complex c = new Complex(0, 0);
        protected override short Calculate(double r, double i, short MaxIteration)
        {
            short j;
            Complex z = c;
            Complex p = new Complex(r, i);

            for (j = 0; j < MaxIteration; j++)
            {
                z = ((z.Sqr() + p - 1) / (2 * z + p - 2)).Sqr();
                if (z.Abs2() > 100 || (z - 1).Abs2() < 0.00005) break;
            }
            return j;
        }
    }
}